<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        #app {
            display: flex;
            flex-direction: column;
        }

        .input-box {
            margin: 10px;
            display: flex;
            align-items: center;
        }

        .input-box input {
            flex: 1;
            height: 40px;
            border: 1px solid #ccc;
            outline: none;
            padding-left: 20px;
            border-radius: 5px;
        }

        .input-box span {
            text-align: center;
            color: lightseagreen;
            width: 60px;
            display: block;
            cursor: pointer;

        }

        .list {
            margin: 10px;
        }

        .list .task {
            margin: 20px 0;
            display: flex;
            justify-content: space-between;
            padding: 15px;
            background-color: red;
            color: white;
        }

        .list .finished {
            background-color: lightseagreen;
        }

        .empty {
            text-align: center;
            margin: 100px 0;
            color: red;
        }

        .btn {
            cursor: pointer;
        }
        .all,
        .would,
        .will {
            width: 100px;
            line-height: 35px;
            text-align: center;
            background-color: lightseagreen;
            color: white;
            border-radius: 5px;
            cursor: pointer;
            float: left;
            margin: 0 10px;
        }

        .will {
            background-color: red;
        }
    </style>
</head>

<body>
    <div id="app">
        <div class="input-box">
            <input type="text" placeholder="输入任务名称" v-model="inputText">
            <span @click="add();finished();unfinish()">新增</span>
        </div>
        <div class="menu">
            <div class="all">全部任务({{list.length}})</div>
            <div class="would">已完成({{would}})</div>
            <div class="will"> 未完成({{will}})</div>
        </div>
        <div class="list">
            <!-- class 绑定 -->
            <!-- :class=" `task ${item.status?'finished':''}` " -->
            <div :class="{ task: true, finished: item.status }" v-for="(item,index) in list" :key="index">
                <input type="checkbox" @click="changeStatus(item);finished();unfinish()">
                <div class="name">{{item.name}}</div>
                <span class="btn" @click="del(index);finished();unfinish()">删除</span>
            </div>
            <div class="empty" v-show="list.length == 0">任务列表为空,请添加一个任务!</div>
        </div>
    </div>
    <script src="./vue-2.5.21.js"></script>
    <script>
        var v = new Vue({
            el: '#app',
            data: {
                inputText: '',//保存输入框中的值
                list: [],
                would: '0',
                will: '0'
            },
            methods: {
                add() { //添加任务
                    if (this.inputText) { //输入框不为空, 才添加任务
                        var obj = { id: this.list.length + 1, name: this.inputText, status: false };
                        this.list.push(obj);
                        this.inputText = '';
                    }
                },
                changeStatus(item) { //更改任务状态, item参数就是 当前点击的任务对象
                    item.status = !item.status;
                },
                del(item) {
                    this.list.splice(item, 1)
                },
                unfinish() {
                    var num2 = 0
                    this.list.forEach((item) => {
                        if (item.status == false) {
                            num2++
                        }
                    });
                    this.will=num2
                },
                finished() {
                    var num1 = 0
                    this.list.forEach((item) => {
                        if (item.status == true) {
                            num1++
                        }
                    });
                    this.would=num1
                },

            }
        })
    </script>
</body>

</html>